﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class InOutBlanaceRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15331;
        protected GridView gvInOutBlanaceRptList;
        protected GridView gvInOutBlanaceRptListShowLot;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnShowLot;
        protected HtmlHead Head1;
        protected Label lblFinanceInOutStockDetailRptList;
        protected Label lblMsg;
        protected Label lblTotalAdjustQty;
        protected Label lblTotalAdjustQtyText;
        protected Label lblTotalAjustAmount;
        protected Label lblTotalAjustAmountText;
        protected Label lblTotalAvailableQty;
        protected Label lblTotalAvailableQtyText;
        protected Label lblTotalBeginningPrice;
        protected Label lblTotalBeginningPriceText;
        protected Label lblTotalBeginningQty;
        protected Label lblTotalBeginningQtyText;
        protected Label lblTotalCurrentIssueInboundPrice;
        protected Label lblTotalCurrentIssueInboundPriceText;
        protected Label lblTotalCurrentIssueInboundQty;
        protected Label lblTotalCurrentIssueInboundQtyText;
        protected Label lblTotalCurrentIssueOutBoundPriceText;
        protected Label lblTotalCurrentIssueOutBoundQty;
        protected Label lblTotalCurrentIssueOutBoundQtyText;
        protected Label lblTotalOutBoundPrice;
        protected Label lblTotalPrice;
        protected Label lblTotalPriceText;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected Label lblTotalReturnitQty;
        protected Label lblTotalReturnitQtyText;
        protected Label lblTotalReturnodUnitPrice;
        protected Label lblTotalReturnodUnitPriceText;
        protected Label lblTotalReturnQty;
        protected Label lblTotalReturnQtyText;
        protected Label lblTotalReturnrdUnitPrice;
        protected Label lblTotalReturnrdUnitPriceText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalAdjustAmount;
        private decimal TotalAdjustQty;
        private decimal TotalAvailableQty;
        private decimal TotalBeginningPrice;
        private decimal TotalBeginningQty;
        private decimal TotalCurrentIssueInboundPrice;
        private decimal TotalCurrentIssueInboundQty;
        private decimal TotalCurrentIssueOutBoundQty;
        private decimal TotalCurrentIssueOutBoundQtyPrice;
        private decimal TotalPrice;
        private decimal TotalQty;
        private decimal TotalReturnitQty;
        private decimal TotalReturnodUnitPrice;
        private decimal TotalReturnQty;
        private decimal TotalReturnrdUnitPrice;
        protected UpdatePanel up1;

        public void BindData2InOutBlanaceRpt(InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(inOutBlanaceRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetInOutBlanaceRptDataSet(inOutBlanaceRptQueryEntity);
            if (inOutBlanaceRptQueryEntity.ShowLot == "Y")
            {
                base.BindData2GridView(this.gvInOutBlanaceRptListShowLot, ds);
            }
            else
            {
                base.BindData2GridView(this.gvInOutBlanaceRptList, ds);
            }
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
            inOutBlanaceRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = inOutBlanaceRptQueryEntity.CurrentPage;
            inOutBlanaceRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetInOutBlanaceRptDataSet(inOutBlanaceRptQueryEntity))
                {
                    if (inOutBlanaceRptQueryEntity.ShowLot == "Y")
                    {
                        base.Export2Excel(" InOutBlanaceRpt.xls", "15331", this.gvInOutBlanaceRptListShowLot, ds);
                    }
                    else
                    {
                        base.Export2Excel(" InOutBlanaceRpt.xls", "15331", this.gvInOutBlanaceRptList, ds);
                    }
                }
            }
            finally
            {
                inOutBlanaceRptQueryEntity.IsGetAll = false;
                inOutBlanaceRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetInOutBlanaceRptQueryEntity(inOutBlanaceRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
            inOutBlanaceRptQueryEntity.CurrentPage = "0";
            this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
            inOutBlanaceRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
            inOutBlanaceRptQueryEntity.OwnerID = "";
            inOutBlanaceRptQueryEntity.SkuID = "";
            inOutBlanaceRptQueryEntity.StartTime = "";
            inOutBlanaceRptQueryEntity.EndTime = "";
            this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
            if (int.Parse(inOutBlanaceRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                inOutBlanaceRptQueryEntity.CurrentPage = (int.Parse(inOutBlanaceRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
            if (int.Parse(inOutBlanaceRptQueryEntity.CurrentPage) > 0)
            {
                inOutBlanaceRptQueryEntity.CurrentPage = (int.Parse(inOutBlanaceRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
            this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
        }

        private InOutBlanaceRptQueryEntity GetInOutBlanaceRptQueryEntity()
        {
            return (base.PageQueryEntitySession as InOutBlanaceRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
                inOutBlanaceRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
            }
        }

        protected void gvInOutBlanaceRptList_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15251", this.gvInOutBlanaceRptList);
        }

        protected void gvInOutBlanaceRptList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblDESCR"));
                }
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblDESCR2"));
                }
                if (((Label) e.Row.FindControl("lblBeginningQty")) != null)
                {
                    this.TotalBeginningQty += decimal.Parse((((Label) e.Row.FindControl("lblBeginningQty")).Text != "") ? ((Label) e.Row.FindControl("lblBeginningQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblBeginningPrice")) != null)
                {
                    this.TotalBeginningPrice += decimal.Parse((((Label) e.Row.FindControl("lblBeginningPrice")).Text != "") ? ((Label) e.Row.FindControl("lblBeginningPrice")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblCurrentIssueInboundQty")) != null)
                {
                    this.TotalCurrentIssueInboundQty += decimal.Parse((((Label) e.Row.FindControl("lblCurrentIssueInboundQty")).Text != "") ? ((Label) e.Row.FindControl("lblCurrentIssueInboundQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblCurrentIssueInboundPrice")) != null)
                {
                    this.TotalCurrentIssueInboundPrice += decimal.Parse((((Label) e.Row.FindControl("lblCurrentIssueInboundPrice")).Text != "") ? ((Label) e.Row.FindControl("lblCurrentIssueInboundPrice")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblCurrentIssueOutBoundQty")) != null)
                {
                    this.TotalCurrentIssueOutBoundQty += decimal.Parse((((Label) e.Row.FindControl("lblCurrentIssueOutBoundQty")).Text != "") ? ((Label) e.Row.FindControl("lblCurrentIssueOutBoundQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblCurrentIssueOutBoundPrice")) != null)
                {
                    this.TotalCurrentIssueOutBoundQtyPrice += decimal.Parse((((Label) e.Row.FindControl("lblCurrentIssueOutBoundPrice")).Text != "") ? ((Label) e.Row.FindControl("lblCurrentIssueOutBoundPrice")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblReturnQty")) != null)
                {
                    this.TotalReturnQty += decimal.Parse((((Label) e.Row.FindControl("lblReturnQty")).Text != "") ? ((Label) e.Row.FindControl("lblReturnQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblReturnrdUnitPrice")) != null)
                {
                    this.TotalReturnrdUnitPrice += decimal.Parse((((Label) e.Row.FindControl("lblReturnrdUnitPrice")).Text != "") ? ((Label) e.Row.FindControl("lblReturnrdUnitPrice")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblReturnitQty")) != null)
                {
                    this.TotalReturnitQty += decimal.Parse((((Label) e.Row.FindControl("lblReturnitQty")).Text != "") ? ((Label) e.Row.FindControl("lblReturnitQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblReturnodUnitPrice")) != null)
                {
                    this.TotalReturnodUnitPrice += decimal.Parse((((Label) e.Row.FindControl("lblReturnodUnitPrice")).Text != "") ? ((Label) e.Row.FindControl("lblReturnodUnitPrice")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblAjustQty")) != null)
                {
                    this.TotalAdjustQty += decimal.Parse((((Label) e.Row.FindControl("lblAjustQty")).Text != "") ? ((Label) e.Row.FindControl("lblAjustQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblAdjustAmount")) != null)
                {
                    this.TotalAdjustAmount += decimal.Parse((((Label) e.Row.FindControl("lblAdjustAmount")).Text != "") ? ((Label) e.Row.FindControl("lblAdjustAmount")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalQty")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblTotalQty")).Text != "") ? ((Label) e.Row.FindControl("lblTotalQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblAvailableQty")) != null)
                {
                    this.TotalAvailableQty += decimal.Parse((((Label) e.Row.FindControl("lblAvailableQty")).Text != "") ? ((Label) e.Row.FindControl("lblAvailableQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalPrice")) != null)
                {
                    this.TotalPrice += decimal.Parse((((Label) e.Row.FindControl("lblTotalPrice")).Text != "") ? ((Label) e.Row.FindControl("lblTotalPrice")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalBeginningQtyText.Text = base.GetPreferenceDigit(this.TotalBeginningQty);
                this.lblTotalBeginningPriceText.Text = base.GetPreferenceDigit(this.TotalBeginningPrice);
                this.lblTotalCurrentIssueInboundQtyText.Text = base.GetPreferenceDigit(this.TotalCurrentIssueInboundQty);
                this.lblTotalCurrentIssueInboundPriceText.Text = base.GetPreferenceDigit(this.TotalCurrentIssueInboundPrice);
                this.lblTotalCurrentIssueOutBoundQtyText.Text = base.GetPreferenceDigit(this.TotalCurrentIssueOutBoundQty);
                this.lblTotalCurrentIssueOutBoundPriceText.Text = base.GetPreferenceDigit(this.TotalCurrentIssueOutBoundQtyPrice);
                this.lblTotalReturnQtyText.Text = base.GetPreferenceDigit(this.TotalReturnQty);
                this.lblTotalReturnrdUnitPriceText.Text = base.GetPreferenceDigit(this.TotalReturnrdUnitPrice);
                this.lblTotalReturnitQtyText.Text = base.GetPreferenceDigit(this.TotalReturnitQty);
                this.lblTotalReturnodUnitPriceText.Text = base.GetPreferenceDigit(this.TotalReturnodUnitPrice);
                this.lblTotalAdjustQtyText.Text = base.GetPreferenceDigit(this.TotalAdjustQty);
                this.lblTotalAjustAmountText.Text = base.GetPreferenceDigit(this.TotalAdjustAmount);
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalAvailableQtyText.Text = base.GetPreferenceDigit(this.TotalAvailableQty);
                this.lblTotalPriceText.Text = base.GetPreferenceDigit(this.TotalPrice);
            }
        }

        protected void gvInOutBlanaceRptList_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = this.GetInOutBlanaceRptQueryEntity();
                inOutBlanaceRptQueryEntity.SortField = e.SortExpression;
                if ((inOutBlanaceRptQueryEntity.SortDirection == "ASC") || (inOutBlanaceRptQueryEntity.SortDirection.Trim() == ""))
                {
                    inOutBlanaceRptQueryEntity.SortDirection = "DESC";
                }
                else if (inOutBlanaceRptQueryEntity.SortDirection == "DESC")
                {
                    inOutBlanaceRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvInOutBlanaceRptListShowLot_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15251", this.gvInOutBlanaceRptListShowLot);
        }

        private void LoadData()
        {
            InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity = new InOutBlanaceRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    inOutBlanaceRptQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    inOutBlanaceRptQueryEntity.SkuID = this.PreviousPage.SkuID;
                    inOutBlanaceRptQueryEntity.SkuClassID = this.PreviousPage.SkuClassID;
                    inOutBlanaceRptQueryEntity.VendorID = this.PreviousPage.VendorID;
                    inOutBlanaceRptQueryEntity.StartTime = this.PreviousPage.StartTime;
                    inOutBlanaceRptQueryEntity.EndTime = this.PreviousPage.EndTime;
                    inOutBlanaceRptQueryEntity.ShowLot = this.PreviousPage.ShoLot;
                    inOutBlanaceRptQueryEntity.LOT_ATTR01 = this.PreviousPage.LotAttribute01;
                    inOutBlanaceRptQueryEntity.LOT_ATTR02 = this.PreviousPage.LotAttribute02;
                    inOutBlanaceRptQueryEntity.LOT_ATTR03 = this.PreviousPage.LotAttribute03;
                    inOutBlanaceRptQueryEntity.LOT_ATTR04 = this.PreviousPage.LotAttribute04;
                    inOutBlanaceRptQueryEntity.LOT_ATTR05 = this.PreviousPage.LotAttribute05;
                    inOutBlanaceRptQueryEntity.LOT_ATTR06 = this.PreviousPage.LotAttribute06;
                    inOutBlanaceRptQueryEntity.LOT_ATTR07 = this.PreviousPage.LotAttribute07;
                    inOutBlanaceRptQueryEntity.LOT_ATTR08 = this.PreviousPage.LotAttribute08;
                    inOutBlanaceRptQueryEntity.LOT_ATTR09 = this.PreviousPage.LotAttribute09;
                    inOutBlanaceRptQueryEntity.LOT_ATTR10 = this.PreviousPage.LotAttribute10;
                    inOutBlanaceRptQueryEntity.EXPIRY_DATE = this.PreviousPage.ExpiryDate;
                    inOutBlanaceRptQueryEntity.PRODUCE_DATE = this.PreviousPage.ProduceDate;
                    inOutBlanaceRptQueryEntity.EXTERNAL_LOT = this.PreviousPage.ExternalLot;
                }
                else
                {
                    inOutBlanaceRptQueryEntity.OwnerID = "";
                    inOutBlanaceRptQueryEntity.SkuID = "";
                    inOutBlanaceRptQueryEntity.StartTime = "";
                    inOutBlanaceRptQueryEntity.EndTime = "";
                    inOutBlanaceRptQueryEntity.ShowLot = "N";
                }
                if (inOutBlanaceRptQueryEntity.ShowLot == "Y")
                {
                    this.gvInOutBlanaceRptListShowLot.Visible = true;
                    this.gvInOutBlanaceRptList.Visible = false;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (inOutBlanaceRptQueryEntity.ShowLot == "Y")
            {
                inOutBlanaceRptQueryEntity.PageSize = this.gvInOutBlanaceRptListShowLot.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvInOutBlanaceRptListShowLot.PageSize;
            }
            else
            {
                inOutBlanaceRptQueryEntity.PageSize = this.gvInOutBlanaceRptList.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvInOutBlanaceRptList.PageSize;
            }
            inOutBlanaceRptQueryEntity.IsGetAll = false;
            inOutBlanaceRptQueryEntity.CurrentPage = "0";
            inOutBlanaceRptQueryEntity.SortField = "IL.OWNER_ID, IL.SKU_ID";
            inOutBlanaceRptQueryEntity.SortDirection = "";
            this.SetInOutBlanaceRptQueryEntity(inOutBlanaceRptQueryEntity);
            this.BindData2InOutBlanaceRpt(inOutBlanaceRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvInOutBlanaceRptList);
                base.InitAnalyticsReportGridview(this.gvInOutBlanaceRptListShowLot);
                this.LoadData();
            }
        }

        public void SetInOutBlanaceRptQueryEntity(InOutBlanaceRptQueryEntity inOutBlanaceRptQueryEntity)
        {
            base.PageQueryEntitySession = inOutBlanaceRptQueryEntity;
        }

        public InOutBlanaceRptQuery PreviousPage
        {
            get
            {
                return (InOutBlanaceRptQuery) base.PreviousPage;
            }
        }
    }
}

